<?php
class Services extends MY_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('model_train');
		$this->load->library("cart");
    }
	public function index(){
		if(!isset($_GET['train'])){die("Page not found!");}
		if($this->cart->total_items() == 0){redirect(base_url()."check-ticket.html");}
		$trainid = (int)$_GET['train'];
		$data['title'] = "Extra services";
		$data['trains'] = $this->listtrains(30);
	    $data['listservice'] = $this->model_train->listservice($trainid);
	    $this->load->view("services/layout",$data);
	}
	public function ajax(){
		//if(!isset($_POST)){die();}
		$name = $_POST['name'];$email = $_POST['email'];
		$phone = $_POST['phone'];$country = $_POST['country'];
		$info = $_POST['info'];
		$serprice = $_POST['serprice']; $sertitle = $_POST['sertitle'];
		$services = serialize(array("price"=>$serprice,"title"=>$sertitle));
		$rule = "/^[a-zA-Z]{1}[a-zA-Z0-9._]{2,}\@[a-zA-Z]{3,}\.[a-zA-Z0-9]{2,8}$/";
		$trainid = "";
		$trainname = "";
		foreach($this->cart->contents() as $items){
			$trainid = $items['options']['trainid']; 
			$trainname = $items['options']['train']; 
			break; die();
		}
		if(!preg_match($rule,$email)){
			echo 1;
		}else{
			$db = array(
				"order_title" => $trainname,
				"order_name" => $this->fillter($name),
				"order_email" => $this->fillter($email),
				"order_phone" => $this->fillter($phone),
				"order_country" => $country,
				"order_ip" => $_SERVER['REMOTE_ADDR'],
				"order_price" => ($this->cart->total() + $serprice),
				"order_info" => $this->fillter($info),
				"order_services" => $services,
				"order_content" => serialize($this->cart->contents()),
				"order_date" => date("d/m/Y - H:i:s"),
				"train_id" => $trainid
			);
			$this->model_train->addorder($db);
			$this->sendmail($db,$email);
			$this->cart->destroy();
		}
	}
	public function sendmail($data,$email){
		$mesnger = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html>
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<style type="text/css">
				body{font:12px Verdana, Geneva, sans-serif;}
			</style>
			</head>
			<body>
			<h2 style="font-size:16px;">BOOKING FROM HANOI - SAPA TRAIN</h2>
			<p style="margin:7px 0px">Thank for your booking, this is your information</p>
			<p>	Full name : '.$data['order_name'].' <br />
				Email address : '.$data['order_email'].' <br />
				Phone number : '.$data['order_phone'].' <br />
				Information : '.$data['order_info'].'
			</p>
			<p style="margin:7px 0px">Your booking</p>
		';
		$mesnger.= "</body></html>";
		send_mail_helper($email,'BOOKING FROM HANOI - SAPA TRAIN',htmlspecialchars_decode($mesnger),$file);
	}
}